package com.sense.kukoo.biz_media.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.utils.TextUtils
import com.sense.feel.common.utils.ToastUtils
import com.sense.kukoo.biz_media.R
import com.sense.kukoo.biz_media.adapter.MediaListDetailAdapter
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.BookDetailsBean
import com.sense.kukoo.common.repo.bean.MediaListDetailItemBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.common.wiget.dialog.ContentAgreementPopupWindow
import com.sense.kukoo.lib_logger.LoggerManager
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet
import com.sense.lib.picture.transformation.BlurTransformation

class MediaTitleProvider(
    val listener: MediaListDetailAdapter.MediaListListener,
):MultiItemProvider<MediaListDetailItemBean>() {

    private val TAG: String = javaClass.simpleName

    private val dp16 = ApplicationContext.dp2px(16F)
    private var mCurrentMusic: LocalMediaListItemBean? = null
    private var mCurrentState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING

    val start = ApplicationContext.getResources().getDrawable(R.drawable.biz_media_icon_listen)
    val pause = ApplicationContext.getResources().getDrawable(R.drawable.biz_media_icon_listen_pause)
    init{
        start.setBounds(0,0,dp16,dp16)
        pause.setBounds(0,0,dp16,dp16)
    }

    override fun getItemType(): Int = MediaListDetailItemBean.TYPE_MEDIA_TITLE

    override fun getItemLayoutId(): Int = R.layout.biz_media_item_media_title

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: MediaListDetailItemBean) {
        val data: BookDetailsBean = bean.bean!!
        //
        val iv_music_icon = viewHolder.getView(R.id.iv_music_icon) as ImageView
        val iv_bg_header_title = viewHolder.getView(R.id.iv_bg_header_title) as ImageView
        Monet.get(ApplicationContext.instance())
            .load<GlideConfig>(data.coverUrl)
            .placeholder(com.sense.kukoo.common.R.mipmap.common_icon_default_editor_picks)
            .into(iv_music_icon)
        Monet.get(ApplicationContext.instance())
            .load<GlideConfig>(data.coverUrl)
            .placeholder(com.sense.kukoo.common.R.mipmap.common_icon_default_editor_picks)
            .transform(BlurTransformation(ApplicationContext.instance(), 10, 20))
            .into(iv_bg_header_title)
        //
        val tv_title = viewHolder.getView(R.id.tv_title) as TextView
        tv_title.text = if (TextUtils.isEmpty(data.alias)) data.name else data.alias
        //
        val tv_music_type = viewHolder.getView(R.id.tv_music_type) as TextView
        when (data.type) {
            BookBean.TYPE_UNKNOW -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_unknow))
            }
            BookBean.TYPE_AUDIOBOOK -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_audiobook))
            }
            BookBean.TYPE_PODCAST -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_podcast))
            }
            BookBean.TYPE_SHORT_AUDIO -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_short_audio))
            }
            else -> {}
        }
        //
        val tv_share_by_name = viewHolder.getView(R.id.tv_share_by_name) as TextView
        tv_share_by_name.text = data.author
        //
        val iv_btn_question = viewHolder.getView(R.id.iv_btn_question) as ImageView
        iv_btn_question.setOnClickListener {
            listener.onBtnQuestionClick(iv_btn_question)
        }
        //
        val tv_listens_content = viewHolder.getView(R.id.tv_listens_content) as TextView
        tv_listens_content.text = "" + data.listeners
        //
        val tv_subscribed_content = viewHolder.getView(R.id.tv_subscribed_content) as TextView
        tv_subscribed_content.text = "" + data.subscribers
        //
        val tv_listening_content = viewHolder.getView(R.id.tv_listening_content) as TextView
        tv_listening_content.text = "" + data.listening
        //
        val tv_btn_listen_bg = viewHolder.getView(R.id.tv_btn_listen_bg)
        tv_btn_listen_bg.setOnClickListener {
            listener.onBtnListenClick(bean)
        }
        //
        val tv_btn_listen = viewHolder.getView(R.id.tv_btn_listen) as TextView
        if(mCurrentMusic != null && mCurrentMusic?.idOfBooks() == data.idOfBook()){
            //同一合集
            when (mCurrentState) {
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_PLAYING ->{
                    tv_btn_listen.setCompoundDrawables(pause,null,null,null)
                    tv_btn_listen.setText(com.sense.kukoo.common_i18n.R.string.media_detail_pause)
                }
                UserWatcher.MusicStateWatcher.STATE_PAUSE ->{
                    tv_btn_listen.setCompoundDrawables(start,null,null,null)
                    tv_btn_listen.setText(com.sense.kukoo.common_i18n.R.string.media_detail_resume)
                }
                else ->{
                    tv_btn_listen.setCompoundDrawables(start,null,null,null)
                    tv_btn_listen.setText(com.sense.kukoo.common_i18n.R.string.media_detail_listen_now)
                }
            }
        }else{
            //不同的
            tv_btn_listen.setCompoundDrawables(start,null,null,null)
            tv_btn_listen.setText(com.sense.kukoo.common_i18n.R.string.media_detail_listen_now)
        }
        //
        if(data.type == BookBean.TYPE_AUDIOBOOK){
            //有声书
            tv_btn_listen.visibility = View.VISIBLE
            tv_btn_listen_bg.visibility = View.VISIBLE
            tv_btn_listen_bg.isEnabled = true
        }else{
            //
            tv_btn_listen.visibility = View.GONE
            tv_btn_listen_bg.visibility = View.INVISIBLE
            tv_btn_listen_bg.isEnabled = false
        }
        //
        val iv_btn_subscribe = viewHolder.getView(R.id.iv_btn_subscribe) as ImageView
        val tv_btn_subscribe_bg = viewHolder.getView(R.id.tv_btn_subscribe_bg)
        val isSubscription:Boolean = 1 == data.isSubscription
        if (isSubscription) {
            iv_btn_subscribe.setImageResource(com.sense.kukoo.common.R.drawable.common_icon2_sub_gray)
            tv_btn_subscribe_bg.background = ApplicationContext.getResources().getDrawable(R.drawable.biz_media_bg_btn_subscribed)
        }else{
            iv_btn_subscribe.setImageResource(com.sense.kukoo.common.R.drawable.common_icon2_sub_red)
            tv_btn_subscribe_bg.background = ApplicationContext.getResources().getDrawable(R.drawable.biz_media_bg_btn_subscribe)
        }
        tv_btn_subscribe_bg.setOnClickListener {
            listener.onBtnSubscribeClick(bean)
        }

        //
        val iv_btn_show_des = viewHolder.getView(R.id.iv_btn_show_des)
        val tv_music_des: TextView = viewHolder.getView(R.id.tv_music_collection) as TextView
        val tv_music_des_all: TextView = viewHolder.getView(R.id.tv_music_des_all) as TextView
        //
        val text = data.introduction
        //
        fun showAll(){
            tv_music_des_all.setText(text)
            tv_music_des_all.visibility = View.VISIBLE
            tv_music_des.visibility = View.GONE
            iv_btn_show_des.visibility = View.GONE
            data.isShowDes = true
        }
        if(data.isShowDes ?: false){
            showAll()
        }else{
            tv_music_des.setText(text)
            tv_music_des_all.visibility = View.GONE
            tv_music_des.visibility = View.VISIBLE
            iv_btn_show_des.visibility = View.VISIBLE
            iv_btn_show_des.setOnClickListener {
                showAll()
            }
        }

        //
        val chapterTotal = data.chapterTotal
        val chapterTotalText = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_controler_episodes, chapterTotal)
        //
        val tv_media_list_title = viewHolder.getView(R.id.tv_media_list_title) as TextView
        tv_media_list_title.setText(chapterTotalText)
        //
        val isSort = data.localAsc == 1
        val iv_btn_media_list_sort = viewHolder.getView(R.id.iv_btn_media_list_sort) as ImageView
        if (isSort) {
            iv_btn_media_list_sort.setImageResource(R.drawable.biz_media_icon_media_sort_down)
        }else{
            iv_btn_media_list_sort.setImageResource(R.drawable.biz_media_icon_media_sort_up)
        }
        iv_btn_media_list_sort.setOnClickListener {
            listener.onBtnSortClick(bean)
        }
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int){
        mCurrentState = state
    }
}